package com.gmzg.gmzgoffice.works;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.test.mock.MockApplication;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gmzg.gmzgoffice.GMZGApplication;
import com.gmzg.gmzgoffice.R;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ClockActivity extends AppCompatActivity {

    @BindView(R.id.toolbar_title)
    TextView mToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.time)
    TextView mTime;
    @BindView(R.id.up_clocl_tv)
    TextView mUpCloclTv;
    @BindView(R.id.up_detail_time)
    TextView mUpDetailTime;
    @BindView(R.id.up_clocl_ll)
    LinearLayout mUpCloclLl;
    @BindView(R.id.enter_tv)
    TextView mEnterTv;
    @BindView(R.id.down_clock_tv)
    TextView mDownClockTv;
    @BindView(R.id.down_detail_time)
    TextView mDownDetailTime;
    @BindView(R.id.down_clock_ll)
    LinearLayout mDownClockLl;
    @BindView(R.id.activity_clock)
    LinearLayout mActivityClock;
    @BindView(R.id.username_tv)
    TextView mUsernameTv;
    private String mCurrentTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clock);
        ButterKnife.bind(this);
        mToolbarTitle.setText("GMZG");
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }

        initView();
    }

    private void initView() {
        //todo：从服务器判断上下班签到按钮的状态，来设置按钮是否可以点击
        long uptimeMillis = System.currentTimeMillis();
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date(uptimeMillis);
        mCurrentTime = df.format(date);
        mTime.setText(mCurrentTime);
        df = new SimpleDateFormat("HH:mm");
        mCurrentTime = df.format(date);
        mUpDetailTime.setText(mCurrentTime);
        mDownDetailTime.setText(mCurrentTime);

        String name = ((GMZGApplication) getApplication()).getUserName();
        mUsernameTv.setText(name);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    public void btn_start_work(View view) {
        String[] split = mCurrentTime.split(":");
        int currentH = Integer.parseInt(split[0]);
        if (currentH>=9) {
            Toast.makeText(this, "今天迟到了，扣除日薪20%", Toast.LENGTH_SHORT).show();
            view.setClickable(false);
        }else {
            Toast.makeText(this, "表现不错，好好工作", Toast.LENGTH_SHORT).show();
            view.setClickable(false);
        }
    }

    public void btn_end_work(View view) {
        String[] split = mCurrentTime.split(":");
        int currentH = Integer.parseInt(split[0]);
        if (currentH < 18){
            Toast.makeText(this, "早退，扣除日薪20%", Toast.LENGTH_SHORT).show();
            view.setClickable(false);
        }else {
            Toast.makeText(this, "表现不错，为年终奖加分", Toast.LENGTH_SHORT).show();
            view.setClickable(false);
        }
    }
}
